package entity;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Version;

/**
 *
 * @author Stefan Martinov <stefan.martinov@gmail.com>
 *
 *         This class is an abstract class created to be inherited by all other
 *         entities which have the need for versioning and logical deletion , or
 *         are modified in any other way by administrators or any other users.
 *         It removes the need to physically delete any objects from database
 *         and removes the problems associated with dirty read or write-over ...
 *
 */
@MappedSuperclass
public abstract class AbstractEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    protected Long id;
    @Version
    @Basic(optional = false)
    @Column(name = "version")
    protected int version = 1;
    @Basic(optional = false)
    @Column(name = "deleted")
    protected boolean deleted = false;


    public int getVersion() {
        return version;
    }

    public void setVersion(int version) {
        this.version = version;
    }

    public boolean isDeleted() {
        return deleted;
    }

    public void setDeleted(boolean deleted) {
        this.deleted = deleted;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public boolean equals(AbstractEntity abs) {
        return (this.getId().equals(abs.getId())
                && this.getVersion() == abs.getVersion()
                && this.isDeleted() == abs.isDeleted());
    }
}
